﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SmApiLibrary.datatypes.Interface;
using System.Data.SqlClient;
using SmApiLibrary.libs;
using System.Data;

namespace Teein.LiNing.DAL
{
    public class AccountDAL : DALBase
    {
        DALBase db = new DALBase();


        public int InsertAccount(IUser user, OAuthBase oauth)
        {
            SqlParameter[] paras = {
                new SqlParameter("@accesstoken",oauth.Token),
                new SqlParameter("@accesstokensecret",oauth.TokenSecret),
                new SqlParameter("@uid",user.id),
                new SqlParameter("@_name",user.name),
                new SqlParameter("@nick",user.name),
                new SqlParameter("@domainName",user.domain),
                new SqlParameter("@countryid",1),
                new SqlParameter("@provinceid",user.province),
                new SqlParameter("@cityid",user.city),
                new SqlParameter("@location",user.location),
                new SqlParameter("@description",user.description),
                new SqlParameter("@blogurl",user.url),
                new SqlParameter("@avatar",user.profile_image_url),
                new SqlParameter("@gender",user.gender),
                new SqlParameter("@followers_count",Convert.ToInt32(user.followers_count)),
                new SqlParameter("@friends_count",Convert.ToInt32(user.friends_count)),
                new SqlParameter("@tweets_count",Convert.ToInt32(user.statuses_count)),
                new SqlParameter("@created_at",user.created_at),
                new SqlParameter("@verified",user.verified=="false" ? 0 :   1 ),
                new SqlParameter("@birth_year",0),
                new SqlParameter("@birth_month",0),
                new SqlParameter("@birth_day",0),
                new SqlParameter("@education",0),
                new SqlParameter("@CreateTime",DateTime.Now)
            };
            int rows = db.myData.ExeSP("Sp_Insert_Accounts", paras, db.ConnStr);
            return rows;
        }

        public int SelectAccountIdByUid(string uid)
        {
            string sql = "SELECT AccountId FROM Accounts WHERE uid=@uid";
            SqlParameter[] paras = { 
                                   new SqlParameter("@uid",SqlDbType.VarChar),
            };

            paras[0].Value = uid;
            int accountId = -1;
            int.TryParse(db.myData.GetScalarStr(sql, paras, db.ConnStr),out accountId);
            return accountId;
        }

    }
}
